package com.longge;

import java.util.*;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-03-16 01:12
 */
public class BSTSequences {

    List<List<Integer>> res = new ArrayList<List<Integer>>();
    LinkedList<Integer> path = new LinkedList<>();

    public List<List<Integer>> BSTSequences(TreeNode root) {
        LinkedList<TreeNode> nodes = new LinkedList<>();
        nodes.add(root);
        dfs(nodes);
        return res;
    }

    private void dfs(LinkedList<TreeNode> nodes) {
        if (nodes.isEmpty()||nodes.get(0)==null) {
            res.add(new ArrayList<>(path));
            return;
        }
        LinkedList<TreeNode> nextNodes = new LinkedList<>(nodes);
        for (TreeNode node : nodes) {
            path.offer(node.val);
            nextNodes.remove(node);
            if (node.left != null) nextNodes.add(node.left);
            if (node.right != null) nextNodes.add(node.right);
            dfs(nextNodes);
            if (node.left != null) nextNodes.remove(node.left);
            if (node.right != null) nextNodes.remove(node.right);
            nextNodes.add(node);
            path.pollLast();
        }
    }

    public static void main(String[] args) {
        LinkedList<Integer> path = new LinkedList<>();

        System.out.println(path);
    }


}
